package com.brayan.melinxa.logic.shadowers.locations;

import java.io.File;
import java.nio.file.FileSystems;
import java.nio.file.InvalidPathException;
import java.nio.file.Path;

import com.brayan.melinxa.logic.exceptions.ShadowerInitalisationException;
import com.brayan.melinxa.logic.shadowers.credentials.LoginCredentials;

public class LocalDirectory implements ShadowerLocation {

	// user String not file. Might be that directory (File object) is not
	// available at startup.
	protected String directoryString = null;

	public LocalDirectory(String localDirString) {
		setDirString(localDirString);
		// Omit to intantiate the directory variable at this place. The
		// filesystem might not be ready. E.g. external harddrive not attached.
	}

	/**
	 * {@inheritDoc}
	 * 
	 * A local directory destination will return null, if asked for login
	 * credentials. Not needed.
	 */
	@Override
	public LoginCredentials getLoginCredentials() {
		return null;
	}

	/**
	 * {@inheritDoc}
	 * 
	 * It's not required to set login credentials. They will not be injected.
	 */
	@Override
	public void setLoginCredentials(LoginCredentials loginCredentials) {

	}

	/**
	 * Returns a canonical string path.
	 * 
	 * @return String Canonical path.
	 */
	public String getDirString() {
		return directoryString;
	}

	/**
	 * Ensure you passe a absolute and canonical path.
	 * 
	 * @param localDirString
	 */
	public void setDirString(String localDirString) {
		this.directoryString = localDirString;
	}

	/**
	 * Grabs the string path of the location and attempts to return a
	 * {@link Path} object.
	 * 
	 * @return Path A possible Path object.
	 * @throws InvalidPathException
	 *             If the location string cannot be mapped to a Path object.
	 */
	public Path getPathObject() throws InvalidPathException {
		Path path = null;
		try {
			path = FileSystems.getDefault().getPath(getDirString());
		} catch (InvalidPathException ipe) {
			// TODO use logger
			System.out.println("Unable to convert internal location string to a path object.");
			throw ipe;
		}
		return path;
	}

	public File getDirectory() throws ShadowerInitalisationException {
		File directory = null;
		try {
			directory = new File(getDirString());
		} catch (Exception ex) {
			// TODO use logger
			throw new ShadowerInitalisationException(
					"The directory string at hand is not (or no longer) valid. " + ex.getMessage());
		}
		return directory;
	}

	/**
	 * {@inheritDoc}
	 * 
	 * Checks if the location is available on the filesystem.
	 */
	public boolean isValid() {
		File directory = null;
		try {
			directory = new File(directoryString);
		} catch (NullPointerException npe) {
			// TODO use logger
			System.out.println("The directory check validated to false. " + npe.getMessage());
			return false;
		}
		
		if (directory.exists() && directory.isDirectory())
			return true;
		else
			return false;
	}

}
